import {createRouter, createWebHashHistory} from 'vue-router';
import ApiPost from "../view/apipost/Index.vue";
import EMail from "../view/email/Index.vue";
import Password from "../view/password/Index.vue";
import Layout from "../layout/Index.vue"
import Login from "../view/login/Index.vue"
import Regist from "../view/regist/Index.vue"
import {useUserStore} from "../store/users.ts";


export const routes: any = [
    {
        path: "/",
        name: "Postwomen",
        component: Layout,
        meta: {title: "apiPost", icon: "Position"},
        redirect: "/apipost",
        children: [{
            path: "/apipost",
            name: "Postwomen",
            component: ApiPost,
            meta: {title: "apiPost", icon: "UserFilled"}
        }]
    },
    {
        path: "/email",
        name: "E-Mail",
        component: Layout,
        meta: {title: "email", icon: "UserFilled"},
        redirect: "/email/email",
        children: [{
            path: "/email/email",
            name: "E-Mail",
            component: EMail,
            meta: {title: "email", icon: "UserFilled"}
        }]
    },
    {
        path: "/password",
        name: "Password Pocket",
        component: Layout,
        meta: {title: "password", icon: "UserFilled"},
        redirect: "/password/password",
        children: [{
            path: "/password/password",
            name: "Password",
            component: Password,
            meta: {title: "password", icon: "UserFilled"}
        }]
    },
    {
        path: "/login",
        name: "Login",
        component: Login,
        meta: {title: "login", icon: "UserFilled", hidden: true}
    },
    {
        path: "/regist",
        name: "Regist",
        component: Regist,
        meta: {title: "Regist", icon: "UserFilled", hidden: true}
    }]
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})
// let isFresh = ref(true)
router.beforeEach((to, _, next) => {
    document.title = to.meta?.title as string
    let token = useUserStore().user.token
    console.log(token, "token")
    if (to.name === "Regist") {
        next()
    }
    if (token || to.name === 'Login') {
        // if (isFresh.value) {
        // next(to.path)
        // } else {
        next()
        // }
        // isFresh.value = false
    } else {
        next('/login?redirect=' + to.path)
    }
})
export default router